﻿using System;
using System.Collections.Generic;
using System.Text;
using RegionInterface;
using Common;

using TaskHandle = Common.TaskHandle;
using RegionClient;
public class RegionContion
{
    public NetworkModule Module;
    public RegionTCP Client;
    private Queue<TaskHandle> TaskList = new Queue<TaskHandle>();
    public RegionContion(NetworkModule module)
    {
        Module = module;
    }
    public void Update()
    {
        while (TaskList.Count > 0)
        {
            TaskList.Dequeue()();
        }
    }
    public void FixedUpdate()
    {

    }
    public float ServerPing()
    {
        if (Client == null) return 0;
        return Client.ServerPing;
    }
    public void LateUpdate()
    {
       
    }
    public void Init(string serverIp, int port, int background, string doMain)
    {
        Client = new RegionTCP(serverIp, port, background, doMain);
        Client.OnMessage += new MessageHandle(client_OnMessage);
        Client.OnPlayersLoad += new Action<bool>(client_OnPlayersLoad);
        Client.OnPlayerLogin += new Action<PlayerData>(client_OnPlayersLogin);
        Client.OnPlayerLogout += new Action<string>(client_OnPlayersLogout);
        Client.OnLogin += delegate(bool result, string message)
        {
            if (result)
            {
                TaskList.Enqueue(delegate()
                {
                    AppInterface.AvatarModule.AddPlayer(true, Client.Player);
                    RegionStateManager.LoginStateChange(LoginState.LoginEnd);
                });
            }
            else
            {
                TaskList.Enqueue(delegate()
                {
                    RegionStateManager.LoginStateChange(LoginState.LoginError);
                });
            }
        };
        Client.OnLogout += delegate(bool result, string message)
        {
            Log.Info(message);
            RegionStateManager.LoginStateChange(LoginState.LogoutEnd);
        };
    }
    public void Close()
    {
        if (Client == null) return;
        if (Client.OnLine)
        {
            RegionStateManager.LoginStateChange(LoginState.LogoutEnd);
            Client.Close();
        }
        Client = null;
    }
    void client_OnMessage(DataObject Msg)
    {
        TaskList.Enqueue(delegate()
        {
            if (Client == null) return;
            Module.OnMessage(ServerType.RegionServer, Msg);
            Client.client.BufferPool.Put(Msg);
        });
    }

    void client_OnPlayersLoad(bool result)
    {
        if (Client == null) return;
        Log.Info("client_OnPlayersLoad");
        Client.GetNextPlayer();
    }
    void client_OnPlayersLogin(PlayerData p)
    {
        if (Client == null) return;
        Log.Info("client_OnPlayersLogin:" + p.PId);
        TaskList.Enqueue(delegate()
        {
            AppInterface.AvatarModule.AddPlayer(false, p);
        });
    }
    void client_OnPlayersLogout(string p)
    {
        if (Client == null) return;
        Log.Info("client_OnPlayersLogout:" + p);
        TaskList.Enqueue(delegate()
        {
            AppInterface.AvatarModule.DeletaPlayer(int.Parse(p));
        });
    }
    public void Login(int id, string name, string para)
    {
        if (Client == null) return;
        if (string.IsNullOrEmpty(para)) throw new Exception("para must be not empty!");
        Client.Login(id, name, para);
        RegionStateManager.LoginStateChange(LoginState.LoginBegin);
    }
    public void RequestPlayers()
    {
        if (Client == null) return;
        Client.RequestPlayers();
    }
}

